package com.dayuanit.booksystem.v3.net;

import com.dayuanit.booksystem.v3.net.common.DYPServer;
import com.dayuanit.booksystem.v3.net.common.DypRequest;
import com.dayuanit.booksystem.v3.net.common.DypResponse;
import com.dayuanit.booksystem.v3.net.common.DypServlet;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端的DYP协议解析
 */
public class Tomdog implements DYPServer {

    /**
     * 启动服务器
     */
    public void start() throws Exception {
        System.out.println("Tomdog服务器已启动...");
        ServerSocket serverSocket = new ServerSocket(9999);
        //服务器会阻塞，直到有客户端连接过来，才会继续往下走；

        //7*24小时不间断的服务
        while (true) {
            Socket clientSocket = serverSocket.accept();

            //为每一个客户端开启一个线程去处理
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        String data = null;
                        while (null != (data = br.readLine())) {
                            //data就是客户端传过来的消息
                            System.out.println(data);

                            try {
                                //交给DYP解析类 进行消息解析工作
                                DypAnalysis dypAnalysis = new DypAnalysis();
                                DypRequest dypRequest = dypAnalysis.analysis(data);

                                //TODO 响应给客户端结果信息  响应的格式：响应内容的编码$响应的内容
                                DypResponse dypResponse = new DypServletResponse("UTF-8", "");

                                //TODO 根据功能编号，调用对应的service方法。
                                if (dypRequest.getFuntionNumber() >= 2000 && dypRequest.getFuntionNumber() <= 2999) {
                                    DypServlet publisherDypServlet = new PublisherDypServlet();
                                    publisherDypServlet.doPost(dypRequest, dypResponse);
                                }

                                if (dypRequest.getFuntionNumber() >= 3000 && dypRequest.getFuntionNumber() <= 3999) {
                                    DypServlet bookDypServlet = new BookDypServlet();
                                    bookDypServlet.doPost(dypRequest, dypResponse);
                                }

                                //通过客户端输出流 将service处理后的结果响应给客户端
                                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
                                bw.write(dypResponse.toString());
                                bw.newLine();
                                bw.flush();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    System.out.println(">>>>>>服务器端线程结束");
                }
            }).start();
        }


    }

    @Override
    public void stop() {
        System.out.println("暂不支持此功能");
    }

    public static void main(String[] args) throws Exception {
        DYPServer tomdog = new Tomdog();
        tomdog.start();
    }
}
